package org.example.lease.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.lease.mapper.LeaseAgreementMapper;
import org.example.lease.model.entities.*;
import org.example.lease.service.*;
import org.example.lease.vo.agreement.AgreementQueryVo;
import org.example.lease.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/12 16:52
 */
@Service
@Slf4j
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;
    @Resource
    private ApartmentInfoService apartmentInfoService;
    @Resource
    private RoomInfoService roomInfoService;
    @Resource
    private PaymentTypeService paymentTypeService;
    @Resource
    private LeaseTermService leaseTermService;

    @Override
    public IPage<AgreementVo> pageLeaseAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo agreementQueryVo) {
        IPage<AgreementVo> agreementVoIPage = leaseAgreementMapper.pageAgreementVoByQuery(page, agreementQueryVo);
        log.info("根据条件查询的租约信息为：{}", agreementVoIPage.getRecords());
        return agreementVoIPage;
    }

    @Override
    public AgreementVo selectLeaseAgreementById(Long id) {
        AgreementVo agreementVo = new AgreementVo();
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement == null) {
            return null;
        }
        BeanUtils.copyProperties(leaseAgreement, agreementVo);
        //和租约有关的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        agreementVo.setApartmentInfo(apartmentInfo);
        //租约上的房间信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        agreementVo.setRoomInfo(roomInfo);
        //租约上的支付方式信息
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        agreementVo.setPaymentType(paymentType);
        //租约的租期信息
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        agreementVo.setLeaseTerm(leaseTerm);
        log.info("agreementVo: {}", agreementVo);
        return agreementVo;
    }

}




